package control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;

import org.apache.log4j.Logger;

import core.corewa.Event;
import db.dao.DictionariesDAO;
import db.dao.LanguagesDAO;
import db.dao.ManagerInDictDAO;
import db.dao.UserDAO;
import db.dao.WordsDAO;
import db.dto.DictionariesDTO;
import db.dto.LanguagesDTO;
import db.dto.ManagerInDictDTO;
import db.dto.UserDTO;
import db.dto.WordsDTO;
import form.ApproverCheckingForm;
import form.BaseForm;
import form.ReviewerDetailForm;

@SuppressWarnings("serial")
public class ApproverHomeController extends BaseController {
	Logger LOG = Logger.getLogger("ApproverHomeController");
	/**
	 * do nothing in here
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procHome(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		return null;
	}
	/**
	 * action when user kick on Check Suggestion button.
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procCheckSuggestion(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		int dictId = Integer.parseInt(request.getParameter("dictId"));
		WordsDAO wordDAO = new WordsDAO();
		List<WordsDTO> listWord = wordDAO.getWord(dictId);
		ApproverCheckingForm approverCheckingForm = new ApproverCheckingForm();
		List<WordsDTO> listNewWords = new ArrayList<WordsDTO>();
		for (WordsDTO wordsDTO : listWord) {
			if(wordsDTO.getStatus() == 0) {
				listNewWords.add(wordsDTO);
			}
		}
		DictionariesDAO dictDAO = new DictionariesDAO();
		DictionariesDTO dictDTO = dictDAO.getDictionary(dictId);
		LanguagesDAO lanDAO = new LanguagesDAO();
		LanguagesDTO from;
		LanguagesDTO to;
		int fromId = dictDTO.getFrom();
		int toId = dictDTO.getTo();
		from = lanDAO.getLanguage(fromId);
		to = lanDAO.getLanguage(toId);
		approverCheckingForm.setDictName(from.getName()+" - "+to.getName());
		approverCheckingForm.setListNewWords(listNewWords);
		approverCheckingForm.setDictId(dictId);
		return approverCheckingForm;
	}
	/**
	 * action when user submit Reviewer Detail button.
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procReviewerDetail(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		int dictId = Integer.parseInt(request.getParameter("dictId"));
		DictionariesDAO dictDAO = new DictionariesDAO();
		DictionariesDTO dictDTO = dictDAO.getDictionary(dictId);
		ReviewerDetailForm result = new ReviewerDetailForm();
		LanguagesDAO lanDAO = new LanguagesDAO();
		LanguagesDTO from = lanDAO.getLanguage(dictDTO.getFrom());
		LanguagesDTO to = lanDAO.getLanguage(dictDTO.getTo());
		result.setDictName(from.getName()+" - "+to.getName());
		result.setDictId(dictId);
		ManagerInDictDAO managerDAO = new ManagerInDictDAO();
		UserDAO userDAO = new UserDAO();
		List<ManagerInDictDTO> listManagerDTO = managerDAO.getManagerInDictDict(dictId);
		List<UserDTO> listReviewer = new ArrayList<UserDTO>();  
		for (ManagerInDictDTO managerInDictDTO : listManagerDTO) {
			UserDTO userDTO = userDAO.getUser(managerInDictDTO.getUserID());
			if(userDTO.getRole() == 1) {
				listReviewer.add(userDTO);
			}
		}
		result.setListReviewer(listReviewer);
		return result;
	}
	
//	public static void main(String args[]) {
//		int dictId = 1;
//		DictionariesDAO dictDAO = new DictionariesDAO();
//		DictionariesDTO dictDTO = dictDAO.getDictionary(dictId);
//		ReviewerDetailForm result = new ReviewerDetailForm();
//		LanguagesDAO lanDAO = new LanguagesDAO();
//		LanguagesDTO from = lanDAO.getLanguage(dictDTO.getFrom());
//		LanguagesDTO to = lanDAO.getLanguage(dictDTO.getTo());
//		result.setDictName(from.getName()+" - "+to.getName());
////		LOG.info("dictName :"+from.getName()+" - "+to.getName());
//		result.setDictId(dictId);
//		ManagerInDictDAO managerDAO = new ManagerInDictDAO();
//		UserDAO userDAO = new UserDAO();
//		List<ManagerInDictDTO> listManagerDTO = managerDAO.getManagerInDictDict(dictId);
//		List<UserDTO> listReviewer = new ArrayList<UserDTO>();  
//		for (ManagerInDictDTO managerInDictDTO : listManagerDTO) {
//			UserDTO userDTO = userDAO.getUser(managerInDictDTO.getUserID());
//			if(userDTO.getRole() == 1) {
//				listReviewer.add(userDTO);
//			}
//		}
//		result.setListReviewer(listReviewer);
//	}
	
	/**
	 * function logout
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procLogout(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		request.getPortletSession().removeAttribute("username", PortletSession.APPLICATION_SCOPE);
		request.getPortletSession().removeAttribute("role", PortletSession.APPLICATION_SCOPE);
		request.getPortletSession().setAttribute("username", "Guest",PortletSession.APPLICATION_SCOPE);
    	request.getPortletSession().setAttribute("role", -1, PortletSession.APPLICATION_SCOPE);
		return null;
	}
}
